﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using WeMakeSP.ImageResizer.Configuration;
using WeMakeSP.ImageResizer.Services.ServiceContract;
using WeMakeSP.ImageResizer.Services.ServiceImplementation;
using WeMakeSP.Platform.SharePoint.Configuration;

namespace WeMakeSP.ImageResizer.UI.Models
{
    /// <summary>
    /// Represents an image resizer settings model.
    /// </summary>
    public class ImageResizerSettingsModel : IImageResizerSettingsModel
    {
        #region Fields

        /// <summary>
        /// The image resizer service field.
        /// </summary>
        private IImageResizerService imageResizerServiceField;

        /// <summary>
        /// The web field.
        /// </summary>
        private SPWeb webField = null;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageResizerSettingsModel"/> class.
        /// </summary>
        private ImageResizerSettingsModel()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageResizerSettingsModel"/> class.
        /// </summary>
        /// <param name="web">The web.</param>
        public ImageResizerSettingsModel(SPWeb web)
            : this()
        {
            // Guard.

            if (web == null)
            {
                throw new ArgumentNullException("web");
            }

            this.webField = web;
        }

        #endregion

        #region Protected Properties

        /// <summary>
        /// Gets the image resizer service.
        /// </summary>
        /// <value>The image resizer service.</value>
        protected IImageResizerService ImageResizerService
        {
            get
            {
                if (this.imageResizerServiceField == null)
                {
                    this.imageResizerServiceField = 
                        new ImageResizerService(this.webField);
                }

                return 
                    this.imageResizerServiceField;
            }
        }

        /// <summary>
        /// Gets the web.
        /// </summary>
        /// <value>The web.</value>
        protected SPWeb Web
        {
            get
            {
                return
                    this.webField;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the picture library configuration.
        /// </summary>
        /// <param name="listId">The list id.</param>
        /// <returns></returns>
        public ImageResizerPictureLibraryConfiguration GetPictureLibraryConfiguration(Guid listId)
        {
            return
                this.ImageResizerService.GetPictureLibraryConfiguration(listId);
        }

        /// <summary>
        /// Sets the picture library configuration.
        /// </summary>
        /// <param name="listId">The list id.</param>
        /// <param name="configuration">The configuration.</param>
        public void SetPictureLibraryConfiguration(Guid listId, ImageResizerPictureLibraryConfiguration configuration) 
        {
            this.ImageResizerService.SetPictureLibraryConfiguration(listId, configuration);
        }

        #endregion
    }
}
